package com.xique.door.project.miniprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.domain.PageResult;
import com.xique.door.bean.domain.Information;
import com.xique.door.mapper.InformationMapper;
import com.xique.door.project.miniprogram.bean.vo.InformationVO;
import com.xique.door.project.miniprogram.service.IMiniInformationService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MiniInformationServiceImpl extends ServiceImpl<InformationMapper, Information>
    implements IMiniInformationService {
  @Override
  public PageResult getInformationList(String informationType, Integer pageNum, Integer pageSize) {
    QueryWrapper<Information> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("information_type", informationType);
    queryWrapper.le("release_starttime", new Date());
    queryWrapper.eq("del", 0);
    queryWrapper.orderByDesc("create_time");
    Page<Information> page = new Page<>();
    page.setCurrent(pageNum);
    page.setSize(pageSize);
    IPage<Information> list = baseMapper.selectPage(page, queryWrapper);
    List<InformationVO> voList = new ArrayList<>();
    if (list.getRecords().size() > 0) {
      for (Information in : list.getRecords()) {
        InformationVO vo = new InformationVO();
        vo.setId(in.getId());
        vo.setTitle(in.getTitle());
        vo.setInformationType(in.getInformationType());
        vo.setCover(in.getCover());
        vo.setContent(in.getContent());
        vo.setReleaseStarttime(in.getReleaseStarttime());
        vo.setViews(in.getViews());
        voList.add(vo);
      }
    }
    long totalPages = list.getPages();
    long totalSize = list.getTotal();
    return PageResult.builder()
        .pageNum(pageNum)
        .pageSize(pageSize)
        .totalPages(totalPages)
        .totalSize(totalSize)
        .list(voList)
        .build();
  }

  @Override
  public AjaxResult getInformationList(String informationType) {
    QueryWrapper<Information> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("information_type", informationType);
    queryWrapper.le("release_starttime", new Date());
    queryWrapper.eq("del", 0);
    queryWrapper.orderByDesc("create_time");
    List<Information> list=baseMapper.selectList(queryWrapper);
    List<InformationVO> voList = new ArrayList<>();
    if (list.size() > 0) {
      for (Information in : list) {
        InformationVO vo = new InformationVO();
        vo.setId(in.getId());
        vo.setTitle(in.getTitle());
        vo.setInformationType(in.getInformationType());
        vo.setCover(in.getCover());
        vo.setContent(in.getContent());
        vo.setReleaseStarttime(in.getReleaseStarttime());
        vo.setViews(in.getViews());
        voList.add(vo);
      }
    }
    return AjaxResult.success(voList);
  }

  @Override
  public AjaxResult getInformation(Long id) {
    Information in= baseMapper.selectById(id);
    InformationVO vo = new InformationVO();
    vo.setId(in.getId());
    vo.setTitle(in.getTitle());
    vo.setInformationType(in.getInformationType());
    vo.setCover(in.getCover());
    vo.setContent(in.getContent());
    vo.setReleaseStarttime(in.getReleaseStarttime());
    vo.setViews(in.getViews());
    in.setViews(in.getViews() + 1);
    baseMapper.updateById(in);
    return AjaxResult.success(vo);
  }

}
